﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KATS_Client.Client;
using System.Threading;

namespace KATS_Client.UI
{
    /// <summary>
    /// A move / copy action
    /// </summary>
    public class Threaded_Queuing_MoveCopy
    {
        /// <summary>
        /// The selected items to move
        /// </summary>
        public List<FileSystemObject> Items = null;

        /// <summary>
        /// The target directory
        /// </summary>
        public FileSystemObject Target = null;

        /// <summary>
        /// Is this action a copy?
        /// </summary>
        bool isCopy;

        /// <summary>
        /// Is this action a copy?
        /// </summary>
        public bool IsCopy
        {
            get { return isCopy; }
            set { this.isCopy = value; }
        }

        /// <summary>
        /// Is this action a move?
        /// </summary>
        public bool IsMove
        {
            get { return !isCopy; }
            set { this.isCopy = !value; }
        }


        /// <summary>
        /// Create a move copy action data file
        /// </summary>
        /// <param name="items">The items to move</param>
        /// <param name="target">The target directory</param>
        /// <param name="isCopy">Is the action a copy?</param>
        public Threaded_Queuing_MoveCopy(List<FileSystemObject> items, FileSystemObject target, bool isCopy)
        {
            this.Items = items;
            this.Target = target;
            this.isCopy = isCopy;
        }

        /// <summary>
        /// Has this thread already started processing?
        /// </summary>
        bool started = false;

        /// <summary>
        /// Gets if this thread has already started processing
        /// </summary>
        public bool Started
        {
            get { return this.started; }
        }

        /// <summary>
        /// Has this thread finished processing?
        /// </summary>
        bool finished = false;

        /// <summary>
        /// Gets if this thread has finished processing
        /// </summary>
        public bool Finished
        {
            get { return this.finished; }
        }

        /// <summary>
        /// Process the Move/Copy action
        /// </summary>
        public void Perform()
        {
            if (started)
                throw new NotSupportedException("Thread already started");

            Thread thread = new Thread(new ThreadStart(ProcessMoveCopyAction));
            thread.Name = "Move/Copy Thread";
            thread.Start();
            
            this.started = true;
        }

        /// <summary>
        /// Process the move copy action and add any revelant data to the queues
        /// </summary>
        /// <param name="data">The action data</param>
        void ProcessMoveCopyAction()
        {
            foreach (FileSystemObject fso in this.Items)
            {
                if (fso.IsDirectory)
                {
                    // then is a directory
                    Client.FileSystemObject source = fso;
                    Client.FileSystemObject target = new Client.FileSystemObject(this.Target.Location.TrimEnd(new char[] { '\\' }) + "\\" + source.Name, this.Target.IsRemote, Client.FileSystemObjectTypes.Directory);
                    if (isCopy)
                    {
                        // copy action
                        Client.FileActionsQueue.CopyFolder(source, target);
                    }
                    else
                    {
                        // move action
                        Client.FileActionsQueue.Move(source, target);
                    }
                }
                else
                {
                    // then is a file
                    Client.FileSystemObject source = fso;
                    Client.FileSystemObject target = new Client.FileSystemObject(this.Target.Location.TrimEnd(new char[] { '\\' }) + "\\" + source.Name, this.Target.IsRemote, Client.FileSystemObjectTypes.File);
                    if (isCopy)
                    {
                        // copy action
                        Client.FileActionsQueue.CopyFile(source, target);
                    }
                    else
                    {
                        // move action
                        Client.FileActionsQueue.Move(source, target);
                    }
                }

            }
            this.finished = true;
        }
    }
}
